package lin.an.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lin.an.food.entity.FoodLike;
import lin.an.food.mapper.FoodLikeMapper;
import lin.an.food.service.FoodLikeService;
import org.springframework.stereotype.Service;

/**
 * @author zhoulinq
 * @date 2025/4/10
 */
@Service
public class FoodLikeServiceImpl extends ServiceImpl<FoodLikeMapper, FoodLike> implements FoodLikeService {




    @Override
    public boolean isLiked(int foodId, int userId) {
        LambdaQueryWrapper<FoodLike> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FoodLike::getFoodId, foodId)
                .eq(FoodLike::getUserId, userId);
        return this.count(queryWrapper) > 0;
    }



    @Override
    public void removeLike(int foodId, int userId) {
            // 1. 从food_like表中删除点赞记录
            LambdaQueryWrapper<FoodLike> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FoodLike::getFoodId, foodId)
                    .eq(FoodLike::getUserId, userId);
           this.remove(queryWrapper) ;

    }

    @Override
    public void addLike(int foodId, int userId) {
        FoodLike foodLike = new FoodLike();
        foodLike.setFoodId(foodId);
        foodLike.setUserId(userId);
        this.save(foodLike);
    }


}